package cn.iocoder.yudao.module.game.service.gameProfitAndLoss;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.game.controller.admin.gameProfitAndLoss.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.gameProfitAndLoss.GameProfitAndLossDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.game.dal.mysql.gameProfitAndLoss.GameProfitAndLossMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.game.enums.ErrorCodeConstants.*;

/**
 * 游戏盈亏 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class GameProfitAndLossServiceImpl implements GameProfitAndLossService {

    @Resource
    private GameProfitAndLossMapper profitAndLossMapper;

    @Override
    public Long createProfitAndLoss(GameProfitAndLossSaveReqVO createReqVO) {
        // 插入
        GameProfitAndLossDO profitAndLoss = BeanUtils.toBean(createReqVO, GameProfitAndLossDO.class);
        profitAndLossMapper.insert(profitAndLoss);
        // 返回
        return profitAndLoss.getId();
    }

    @Override
    public void updateProfitAndLoss(GameProfitAndLossSaveReqVO updateReqVO) {
        // 校验存在
        validateProfitAndLossExists(updateReqVO.getId());
        // 更新
        GameProfitAndLossDO updateObj = BeanUtils.toBean(updateReqVO, GameProfitAndLossDO.class);
        profitAndLossMapper.updateById(updateObj);
    }

    @Override
    public void deleteProfitAndLoss(Long id) {
        // 校验存在
        validateProfitAndLossExists(id);
        // 删除
        profitAndLossMapper.deleteById(id);
    }

    private void validateProfitAndLossExists(Long id) {
        if (profitAndLossMapper.selectById(id) == null) {
            throw exception(PROFIT_AND_LOSS_NOT_EXISTS);
        }
    }

    @Override
    public GameProfitAndLossDO getProfitAndLoss(Long id) {
        return profitAndLossMapper.selectById(id);
    }

    @Override
    public PageResult<GameProfitAndLossDO> getProfitAndLossPage(GameProfitAndLossPageReqVO pageReqVO) {
        return profitAndLossMapper.selectPage(pageReqVO);
    }

}